
// 参数/变量名
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/ImageHelper.dart';
import 'Constant.dart';
import 'package:ysy_flutter_mvvm_yuanxuan/helper/Constant.dart';
import 'ImageHelper.dart';

// <string name="space">\u3000</string>
// <string name="space1">\u0020</string>
// <string name="space1">\u200B</string>
class ViewDufColor extends ColorSwatch<String> {

  const ViewDufColor(int primary, Map<String, Color> swatch) : super(primary, swatch);

  static const int defPriceTextColor=ConstantColors.mainTextColor;

  static const ViewDufColor priceMain = ViewDufColor(defPriceTextColor,
    <String, Color>{
      "priceMain": Color(defPriceTextColor),//价格展示颜色
    },
  );
  static const ViewDufColor unitMain = ViewDufColor(0xFF999999,
    <String, Color>{
      "unitMain": Color(0xFF999999),//单位展示颜色
    },
  );
  static const ViewDufColor saleMain = ViewDufColor(0xFF8F91A4,
    <String, Color>{
      "saleMain": Color(0xFF8F91A4),//销量展示颜色
    },
  );
}

class GenericViewParameters{

  static const double bgRoundedCorners=8;

  static const double imageRoundedCorners=4;

}

//弹窗 -Center
showCenterToast(String msg){
  Fluttertoast.cancel();
  Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.CENTER,
      timeInSecForIosWeb: 1,
      backgroundColor: Color(0xffeeeeee),
      textColor: Colors.black54,
      fontSize: 16.0
  );
}

//弹窗 -Bottom
showBottomToast(String msg){
  Fluttertoast.cancel();
  Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
      timeInSecForIosWeb: 1,
      backgroundColor: Color(0xffeeeeee),
      textColor: Colors.black54,
      fontSize: 16.0
  );
}


// 询价 和 单位处理  销售量
class PriceInfoDealWith extends StatelessWidget {

  double? symbolSize;
  Color? symbolColor;

  double? priceSize;
  Color? priceColor;
  String? priceText;

  double? unitSize;
  Color? unitColor;
  String? unitText;

  double? saleSize;
  Color? saleColor;
  String? saleText;

  int? isAskPriceFlag; //是否询价  0 否 1 是
  String? askPriceTag; //询价展示文字

  PriceInfoDealWith({
    this.symbolSize,
    this.symbolColor=ViewDufColor.priceMain,

    this.priceSize,
    this.priceColor=ViewDufColor.priceMain,
    required this.priceText,

    this.unitSize,
    this.unitColor=ViewDufColor.unitMain,
    this.unitText="",

    this.saleSize,
    this.saleColor=ViewDufColor.saleMain,
    this.saleText="",

    this.isAskPriceFlag=0,
    this.askPriceTag,
      });

  @override
  Widget build(BuildContext context) {
    return Text.rich(
        TextSpan(children: [
          TextSpan(
              text: isAskPriceFlag!=1?"¥":"",
              style: TextStyle(
                  color: symbolColor,
                  fontSize: ScreenUtil().setSp(symbolSize??12))),
          TextSpan(
              text: isAskPriceFlag!=1?"${priceText??"--"}":(askPriceTag??"询价"),
              style: TextStyle(
                  color: priceColor,
                  fontSize: ScreenUtil().setSp(priceSize??16))),
          TextSpan(
              text: "${unitText??""}",
              style: TextStyle(
                  color: unitColor,
                  fontSize: ScreenUtil().setSp(unitSize??10))),
          TextSpan(
              text: "    ",
              style: TextStyle(
                  color: unitColor,
                  fontSize: ScreenUtil().setSp(unitSize??10))),
          TextSpan(
              text: "${saleText??""}",
              style: TextStyle(
                  color: saleColor,
                  fontSize: ScreenUtil().setSp(saleSize??10))),
        ]),
        textAlign: TextAlign.left,
        overflow: TextOverflow.ellipsis,
        maxLines: 1);
  }
}

//通用标题
class DufTitleBar extends StatelessWidget {

  Color? backColor;
  String? backIc;
  // backWidget 和 「backColor backIc 」不共存
  Widget? backWidget;
  VoidCallback? callback;

  double? titleSize;
  Color? titleColor;
  String? titleText;

  // titleWidget 和 「titleSize titleColor titleText」不共存
  Widget? titleWidget;

  Widget? more;

  Color color;

  DufTitleBar({this.backColor, this.backIc, this.backWidget,this.callback,this.titleWidget, this.titleSize,
      this.titleColor, this.titleText, this.more, this.color=Colors.transparent});

  @override
  Widget build(BuildContext context) {

    Widget back= InkWell(
         onTap: (){callback??Navigator.pop(context);},
         child: Container(width: 42.sp,child: backWidget?? Image.asset(assetPath(backIc??"ic_arrow_left_black_bold.png"),height: 18.h,color: backColor??Color(0xff8F91A4),fit: BoxFit.fitHeight,)),);
    Widget title= titleWidget?? TextButton(
        onPressed: (){},
        child: titleWidget??Text(titleText??"",
        textAlign: TextAlign.center,
        style: TextStyle(fontSize: titleSize??18.sp,fontWeight: FontWeight.w600,color: titleColor??Color(0xff333333)),));

    return IntrinsicHeight(
      child: Container(
        alignment: Alignment.center,
        color: color,
        padding: EdgeInsets.fromLTRB(12.w, ScreenUtil().statusBarHeight+3.sp, 12.w, 3.sp),
        child: Container(
          height: 46.sp,
          padding: EdgeInsets.zero,
          alignment: Alignment.center,
          child: Stack(
              alignment: Alignment.centerLeft,
              children: [
                Container(child: IntrinsicWidth(child: back),alignment: Alignment.centerLeft,),
                Center(child: title,widthFactor: double.infinity,),
                Container(child: more,width: double.infinity,alignment: Alignment.centerRight,),
              ],
          ),
        ),
      ),
    );
  }
}

//图片默认外围 圆角
class DufRoundedWidget extends StatelessWidget{

  Widget child;

  DufRoundedWidget(this.child);

  @override
  Widget build(BuildContext context) {
      return ClipRRect(
          borderRadius: BorderRadius.all(Radius.circular(GenericViewParameters.imageRoundedCorners)),
          child:child);
  }
}

// class DufCheckbox extends StatelessWidget {
//
//   ValueChanged<bool?>? onChanged;
//   double splashRadius;
//   bool value;
//
//   DufCheckbox( {required this.value,required this.splashRadius,this.onChanged,});
//
//   @override
//   Widget build(BuildContext context) {
//     return SizedBox(
//       width: splashRadius*2+4.sp,height: splashRadius*2+2.sp,
//       child: Checkbox(
//           shape:CircleBorder(),
//           focusColor:value?Color(ConstantColors.mainColor):Color(0xffdddddd),
//           checkColor: Colors.white,
//           materialTapTargetSize:MaterialTapTargetSize.shrinkWrap,
//           visualDensity:VisualDensity(horizontal: 3, vertical: 3),
//           fillColor: MaterialStateProperty.all(value?Color(ConstantColors.mainColor):Color(0xffdddddd)),
//           splashRadius: splashRadius,
//           value: value,
//           onChanged: onChanged
//       ),
//     );
//   }
// }

// class DufCheckbox extends StatelessWidget {
//
//   ValueChanged<bool?>? onChanged;
//   double splashRadius;
//   bool value;
//
//   DufCheckbox( {required this.value,required this.splashRadius,this.onChanged,});
//
//   @override
//   Widget build(BuildContext context) {
//     return InkWell(
//       onTap: (){
//         if(onChanged!=null){
//           value=!value;
//           onChanged!(value);
//         }
//       },
//       child: Container(
//         width: splashRadius*2+4.sp,height: splashRadius*2+2.sp,
//         child: Image.asset(assetPath("ic_checkbox_non.png")),
//       ),
//     );
//   }
// }

class DufCheckbox extends StatefulWidget {

  ValueChanged<bool>? onChanged;
  double radius;//显示大小
  double? tapHeight;//点击范围大小
  double? tapWidth;//点击范围大小
  bool value;

  DufCheckbox( {required this.value,required this.radius,this.onChanged,this.tapHeight,this.tapWidth});

  @override
  _DufCheckboxState createState() => _DufCheckboxState();
}

class _DufCheckboxState extends State<DufCheckbox> {

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: (){
        if(widget.onChanged!=null){
          widget.value=!widget.value;
          widget.onChanged!(widget.value);
          setState(() {});
        }
      },
      child: Container( width: widget.tapWidth??widget.radius*2+16.sp,height: widget.tapHeight??widget.radius*2+16.sp,alignment: Alignment.center,
          child: Image.asset(assetPath(widget.value?"ic_checkbox_sel.png":"ic_checkbox_non.png"),width: widget.radius*2+2.sp,height: widget.radius*2+2.sp,fit: BoxFit.fitHeight,)),
    );
  }
}



//列表 店铺 标题
class ListStoreTitle extends StatelessWidget{

  double imageWidget;
  double imageHeight;
  String imageUrl;

  double? storeFontSize;
  Color? storeFontColor;
  String storeName;

  double? describeSize;
  Color? describeColor;
  String describeName;

  ListStoreTitle({
      this.imageWidget=48,
      this.imageHeight=48,
      required this.imageUrl,
      this.storeFontSize,
      this.storeFontColor,
      required  this.storeName,
      this.describeSize,
      this.describeColor,
      required this.describeName});

  @override
  Widget build(BuildContext context) {
      return Container(
          height: ScreenUtil().setWidth(imageHeight),
          child: Row(
            children: [
              DufRoundedWidget(assetImage(imageUrl,width: ScreenUtil().setWidth(imageWidget),height: ScreenUtil().setWidth(imageHeight))),
              Padding(padding: EdgeInsets.fromLTRB(0, 0, 8.w, 0)),
              Expanded(child: Column(
                 mainAxisAlignment: MainAxisAlignment.center,
                 crossAxisAlignment: CrossAxisAlignment.start,
                 children: [
                   Expanded(
                      child: Container(
                        padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                        alignment: Alignment.bottomLeft,
                        child: Text("$storeName",
                          textAlign: TextAlign.end,
                          style: TextStyle(color: storeFontColor??Color(0xff333333),fontSize: ScreenUtil().setSp(storeFontSize??16),fontWeight: FontWeight.w600)),
                      ),
                  ),
                   Expanded(
                      child: Container(
                        padding: EdgeInsets.fromLTRB(0, 2, 0, 0),
                        alignment: Alignment.topLeft,
                        child: Text("$describeName",
                        textAlign: TextAlign.end,
                        style: TextStyle(color: describeColor??Color(0xff999999),fontSize: ScreenUtil().setSp(describeSize??12))),
                      ),),
                 ],)
              )
            ],
          ),
      );
  }

}


//商品 标题  -横向
class GoodsHTitle extends StatelessWidget{

  double imageWidget;
  double imageHeight;
  String imageUrl;

  double? storeFontSize;
  Color? storeFontColor;
  String goodsName;
  String priceText;
  String unitText;


  GoodsHTitle({
    this.imageWidget=50,
    this.imageHeight=50,
    required this.imageUrl,
    this.storeFontSize,
    this.storeFontColor,
    required  this.goodsName,
    required  this.priceText,
    required  this.unitText});

  @override
  Widget build(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.all(Radius.circular(GenericViewParameters.imageRoundedCorners) ),
      child: Container(
        height: ScreenUtil().setWidth(imageHeight),
        child: Row(
          children: [
            assetImage(imageUrl,width: ScreenUtil().setWidth(imageWidget),height: ScreenUtil().setWidth(imageHeight)),
            Expanded(child: Container(
              color: Color(0xffF5F6F8),
              padding: EdgeInsets.fromLTRB(12.w, 0, 12.w, 0),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                      alignment: Alignment.bottomLeft,
                      child: Text("$goodsName",
                          textAlign: TextAlign.end,
                          style: TextStyle(color: storeFontColor??Color(0xff2D3137),fontSize: ScreenUtil().setSp(storeFontSize??13),fontWeight: FontWeight.w600)),
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.fromLTRB(0, 2, 0, 0),
                      alignment: Alignment.topLeft,
                      child: PriceInfoDealWith(priceText: priceText,unitText:unitText),))
                ],),
            )
          )],
        ),
      ),
    );
  }
}


//带有 右上 数字标签
class RightTag extends StatefulWidget {

  ValueNotifier<int> valueNotifier=ValueNotifier<int>(0);
  Widget child;
  double? width;
  double? height;

  RightTag({required this.valueNotifier,required this.child, this.width, this.height});

  @override
  _RightTagState createState() => _RightTagState();
}

class _RightTagState extends State<RightTag> {

  @override
  void dispose() {
    super.dispose();
    widget.valueNotifier.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width,
      height: widget.height,
      alignment: Alignment.center,
      child: IntrinsicWidth(
        child: IntrinsicHeight(
          child: Stack(
              alignment: Alignment.center,
              children: [
                Padding(
                  padding:  EdgeInsets.fromLTRB(8.r, 8.r, 8.r, 0),
                  child: widget.child,
                ),
                Container(
                  alignment: Alignment.topRight,
                  child: ValueListenableBuilder<int>(
                    valueListenable: widget.valueNotifier,
                    builder: (BuildContext context,int value, Widget? child){
                      return value==0?Text(""):CircleAvatar(
                          radius: 8.r,
                          backgroundColor: Color(0xffF32737),
                          child: Text("${value>99?"99+":value}",style: TextStyle(fontSize:value>99?8.sp:10.sp,color: Colors.white),));
                    },
                  ),
                ),
              ]
          ),
        ),
      ),
    );
  }
}




